package com.cema.manage.project.manage.antiCheat.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.project.manage.antiCheat.domain.AntiCheat;
import com.cema.manage.project.manage.antiCheat.domain.AntiCheatVo;
import com.cema.manage.project.manage.antiCheat.service.IAntiCheatService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;


/**
 * 反作弊 信息操作处理
 *
 * @author reasahi
 * @date 2024-07-15
 */
@Controller
@RequestMapping("/manage/antiCheat")
public class AntiCheatController extends BaseController {
    private String prefix = "manage/antiCheat";

    @Autowired
    private IAntiCheatService antiCheatService;

    @GetMapping()
    @RequiresPermissions("manage:antiCheat:view")
    public String antiCheat() {
        return prefix + "/antiCheat";
    }

    /**
     * 查询反作弊列表
     */
    @RequiresPermissions("manage:antiCheat:list")
    @ResponseBody
    @PostMapping("/list")
    public TableDataInfo list(AntiCheatVo antiCheatVo) {
        startPage();
        EntityWrapper<AntiCheat> entityWrapper = new EntityWrapper<>();
        Date startTime = antiCheatVo.getStartTime();
        AntiCheat antiCheat = BeanUtil.toBean(antiCheatVo, AntiCheat.class);
        entityWrapper.setEntity(antiCheat);
        if (ObjectUtil.isNotNull(startTime)) {
            entityWrapper.between("update_time", startTime, antiCheatVo.getEndTime());
        }
        String startRiskScore = antiCheatVo.getStartRiskScore();
        String endRiskScore = antiCheatVo.getEndRiskScore();
        if (StrUtil.isNotEmpty(startRiskScore) && StrUtil.isNotEmpty(endRiskScore)) {
            entityWrapper.between("risk_score", startRiskScore, Integer.parseInt(endRiskScore) + 1);
        }
        if (StrUtil.isNotEmpty(startRiskScore)) {
            entityWrapper.ge("risk_score", startRiskScore);
        }
        if (StrUtil.isNotEmpty(endRiskScore)) {
            entityWrapper.le("risk_score", endRiskScore);
        }
        entityWrapper.orderBy("update_time", false);
        List<AntiCheat> list = antiCheatService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增反作弊
     */
    @RequiresPermissions("manage:antiCheat:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改反作弊
     */
    @RequiresPermissions("manage:antiCheat:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        AntiCheat antiCheat = antiCheatService.selectById(id);
        model.addAttribute("antiCheat", antiCheat);
        return prefix + "/edit";
    }

    /**
     * 保存反作弊
     */
    @RequiresPermissions("manage:antiCheat:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(AntiCheat antiCheat) {
        Integer id = antiCheat.getId();
        if (StringUtils.isNotNull(id)) {
            if (antiCheatService.updateById(antiCheat)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (antiCheatService.insert(antiCheat)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除反作弊
     */
    @RequiresPermissions("manage:antiCheat:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (antiCheatService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除反作弊
     */
    @RequiresPermissions("manage:antiCheat:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[] ids) {
        List list = new ArrayList();
        for (Integer n : ids) {
            list.add(n);
        }
        if (antiCheatService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

}
